<?php

class Application_Model_BoxUnitMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_BoxUnit');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_UserAddress $address)
	{
		$data = array(
				'id' => $address->getId(),
				'box_id' => $address->getBoxId(),
				'unit_num' => $address->getUnitNum(),
				'unit_status' => $address->getUnitStatus(),
				'size_id' => $address->getSizeId(),
				

				//'created' => date('Y-m-d H:i:s'),
		);
	
		if (null === ($id = $address->getId())) {
			unset($data['id']);
			$this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}
	
	public function find($id)
	{
		$unit = new Application_Model_BoxUnit();
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$unit->setId($row->id)
		->setBoxId($row->box_id )
		->setUnitNum($row->unit_num)
		->setUnitStatus($row->unit_status)
		->setSizeId($row->size_id)
		
		//->setEmail($row->email)
		//
		//->setPassword($row->password)
		//->setCreated($row->created)
		;
		return $unit;
	}
	
	public function findAvaliableByAreaId($size_id, $areaid){
		//find box lists by area_id
		//for each box find unit by size_id
		$boxMapper = new Application_Model_BoxMapper();
		$boxs = $boxMapper->findByAreaId($areaid);
		foreach($boxs as $box){
			$box_id = $box->getId();
			$unit = $this->findAvaliableByBoxId($size_id, $box_id);
			if($unit){
				return $unit;
			}
		}
		
		
	}
	
	public function findAvaliableByBoxId($size_id,$box_id){
		//check box status first then check unit avaliability
		$boxMapper = new Application_Model_BoxMapper();
		$box = $boxMapper->find($box_id);
		if($box->getOnline() == 0){
			//if box not online return false
			return false;
		} 
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		$where = $db->quoteInto('box_id = ?', $box_id).$db->quoteInto(' AND size_id = ?', $size_id).$db->quoteInto(' AND unit_status = ?', 'avaliable');
		
		$row = $table->fetchRow($where);
		try {
			$row = $table->fetchRow($where);
		} catch (Exception $e) {
			echo $e;
		}
		/* $entries   = array();
		foreach ($resultSet as $row) { */
			$entry = new Application_Model_BoxUnit();
			$entry->setId($row->id)
			->setBoxId($row->box_id )
			->setUnitNum($row->unit_num)
			->setUnitStatus($row->unit_status)
			->setSizeId($row->size_id)
			;
			
			return $entry;
			/* $entries[] = $entry;
		}
		return $entries; */
	}
	
	
	public function fetchAll()
	{
	$resultSet = $this->getDbTable()->fetchAll();
	$entries   = array();
	foreach ($resultSet as $row) {
	$entry = new Application_Model_BoxUnit();
	$entry->setId($row->id)
		->setBoxId($row->box_id )
		->setUnitNum($row->unit_num)
		->setUnitStatus($row->unit_status)
		->setSizeId($row->size_id)
	;
	$entries[] = $entry;
	}
	return $entries;
	}

}

